import threading
import time
from datetime import datetime
import pic_name
import queue
import os
import requests
from lxml import etree



def pc(q, name1):
    i = q.get()
    name = name1 + '/' + i.split("/")[-1]
    response = requests.get(i)
    with open(name, 'wb') as f:
        f.write(response.content)
    print(name + '下载完成')
    if q.qsize() != 0:
        pc(q, name1)


def main():
    q = queue.Queue()
    pic_names, un = pic_name.main()
    # http://www.wn01.lol/photos-index-aid-217235.html
    url = 'http://www.wn01.lol/photos-index-aid-{gg}.html'.format(gg=un)
    response = requests.get(url)
    root = etree.HTML(response.content)
    print(root)

    # 名字
    name = root.xpath('//*[@id="bodywrap"]/h2/text()')
    name = name[0]

# 创建文件夹
    filepath = name
    if not os.path.isdir(filepath):
        # 创建文件夹
        os.mkdir(filepath)

    # 图片url切割
    num = url[-11:-5]
    num1 = num[:-2]
    num2 = num[-2:]
    num = num1 + "/" + num2 + "/"

    # 拼接为图片下载地址
    uuu = input("图片前缀:img3.qy0.ru or img4.qy0.ru///")
    for i in pic_names:
        s = i + '.jpg'

        Nurl = "{uu}data/".format(uu=uuu) + num + s
        q.put(Nurl)


    max_connections = 20  # 定义最大线程数
    pool_sema = threading.BoundedSemaphore(max_connections)  # 或使用Semaphore方法


    def loop(i):  # 任务
        pool_sema.acquire()  # 加锁，限制线程数
        print('assignment %s start %s' % (i, datetime.datetime.today()))
        time.sleep(2)
        print('assignment %s end %s' % (i, datetime.datetime.today()))
        pool_sema.release()  # 解锁

    thread_list = []
    for i in range(12):  # 12个任务
        t = threading.Thread(target=pc, args=(q,name,))
        thread_list.append(t)

    for t in thread_list:
        t.start()  # 调用start()方法，开始执行

    for t in thread_list:
        t.join()  # 子线程调用join()方法，使主线程等待子线程运行完毕之后才退出


if __name__ == '__main__':
    main()
